﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// qiugou_detail 的摘要说明
    /// </summary>
    public class xunjia_detail : IHttpHandler, IRequiresSessionState
    {
        protected int id = 0;
        public void ProcessRequest(HttpContext context)
        {
            id = DTRequest.GetQueryInt("id", 0);
            string _systemType = DTRequest.GetString("systemType");
            //Utils.WriteTextLog("xunjia_detail", "_systemType:" + _systemType , DateTime.Now);
            if (id == 0)
            {
                return;
            }
            //检查用户是否登录
            string user_name = DTRequest.GetFormString("txtUserName");
            string user_pwd = DTRequest.GetFormString("txtPassword");
            Model.users uModel = new BLL.users().GetModel(user_name, user_pwd, 0, 1, true);
            BLL.users blluser = new BLL.users();
            BLL.xunjia bll = new BLL.xunjia();
            
            Model.xunjia model = bll.GetModel(id);
            //#####根据用户会员级别查看会员委托求购信息详情电话的查看次数
            //##### 2018 11 01
            BLL.user_groups bllgroup = new BLL.user_groups();
            

            //更新阅读数
            bll.UpdateField(id, "hits=hits+1");

            string tips = "";
            string mobile = "";
            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            strTxt.Append("\"user_id\":" + model.user_id);
            strTxt.Append(",\"remark\":\"" + Microsoft.JScript.GlobalObject.escape(model.remark) + "\"");
            strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(model.add_time)) + "\"");
            strTxt.Append(",\"hits\":" + model.hits);
            //strTxt.Append(",\"umodel\":" + uModel.user_name+","+uModel.group_id);
            if (model.user_id > 0)
            {
                Model.users userModel = new BLL.users().GetModel(model.user_id);
                if (userModel != null)
                {
                    strTxt.Append(",\"lianxiren\":\"" + userModel.user_name + "\"");
                    if (string.IsNullOrEmpty(userModel.avatar))
                    {
                        strTxt.Append(",\"avatar\":\"../image/default_avatar.png\"");
                    }
                    else if (userModel.avatar.Contains("shicaiquanzi"))
                    {
                        strTxt.Append(",\"avatar\":\"http://" + userModel.avatar + "\"");
                    }
                    else
                    {
                        strTxt.Append(",\"avatar\":\"http://pic.shicaiquanzi.com" + userModel.avatar + "\"");
                    }
                    if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(user_pwd) || uModel == null || uModel.group_id < 2)
                    {
                        strTxt.Append(",\"mobile\":\"请联系客服开通VIP\"");
                        mobile += "未登录或者会员为普通会员\"mobile\":\"请联系客服开通VIP\"";
                    }
                    else
                    {
                        if( _systemType!= "android") //_systemType!=""&&
                        {

                            
                            #region  老版本的信息查看计数逻辑 应苹果版本审核不通过原因，修改苹果版本为原来逻辑 苹果本次审核成功后去掉
                            int userViewCount = bllgroup.GetGongqiuView(uModel.group_id);
                            int group_1view111 = bllgroup.GetGongqiuView(2);
                            int group_2view111 = bllgroup.GetGongqiuView(3);
                            //if (group_1view == 0) { group_1view = 1; }
                            //if (group_2view == 0) { group_2view = 3; }
                            int group_1view = 1;
                            int group_2view = 3;
                            BLL.view_log viewbll = new BLL.view_log();
                            if (uModel.group_id == 2) { userViewCount = 1; }
                            else if (uModel.group_id == 3) { userViewCount = 3; }
                            int rzcount = bllgroup.GetGongqiuView(uModel.group_id);
                            //###认证会员 只能查看一次 
                            //###钻石会员 钻石 3条
                            //### Demon 2018-10-30
                            //if (uModel.group_id > 1)
                            //{
                            //    if (uModel.readtime != null)
                            //    {
                            //        if (uModel.readtime.Date == DateTime.Now.Date)
                            //        {
                            //            if (uModel.readcount >= userViewCount)//认证会员 只能查看一次 
                            //            {
                            //                strTxt.Append("\"mobile\":\"请联系客服开通VIP\"");
                            //                if (uModel.group_id == 2)
                            //                    tips = "认证会员活动期可以体验每天查看" + group_1view + "次的权利,升级VIP 可以每天查看" + group_2view + "次,赶紧联系客服升级吧";
                            //            }
                            //            else
                            //            {
                            //                if (bll.CheckPhoneNum(id) == "error")
                            //                {
                            //                    strTxt.Append("\"mobile\":\"请联系客服开通VIP\"");
                            //                    tips = "该条信息已经被访问超过二十次,拒绝访问!";
                            //                }
                            //                else
                            //                {
                            //                    strTxt.Append("\"mobile\":\"" + userModel.mobile + "\"");
                            //                    blluser.UpdateField(uModel.id, "readcount=readcount+1");
                            //                    viewbll.AddLog(1, uModel.id, uModel.user_name, model.id, model.remark, "从xunjia_check中查看", DateTime.Now);
                            //                }
                            //                Utils.WriteTextLog("xunjia_check-" + id, "用户" + user_name + "查看该条信息；" + tips, DateTime.Now);

                            //            }
                            //        }
                            //        else
                            //        {
                            //            if (bll.CheckPhoneNum(id) == "error")
                            //            {
                            //                strTxt.Append("\"mobile\":\"请联系客服开通VIP\"");
                            //                tips = "该条信息已经被访问超过二十次,拒绝访问!";
                            //            }
                            //            else
                            //            {
                            //                strTxt.Append("\"mobile\":\"" + userModel.mobile + "\"");
                            //                blluser.UpdateField(uModel.id, "readcount=1,readtime=getdate()");
                            //                viewbll.AddLog(1, uModel.id, uModel.user_name, model.id, model.remark, "从xunjia_check中查看", DateTime.Now);
                            //            }
                            //            Utils.WriteTextLog("xunjia_check-" + id, "用户" + user_name + "查看该条信息；" + tips, DateTime.Now);
                            //        }
                            //    }
                            //    else
                            //    {
                            //        if (bll.CheckPhoneNum(id) == "error")
                            //        {
                            //            strTxt.Append("\"mobile\":\"请联系客服开通VIP\"");
                            //            tips = "该条信息已经被访问超过三十次,拒绝访问!";
                            //        }
                            //        else
                            //        {
                            //            strTxt.Append("\"mobile\":\"" + userModel.mobile + "\"");
                            //            blluser.UpdateField(uModel.id, "readcount=1,readtime=getdate()");
                            //            viewbll.AddLog(1, uModel.id, uModel.user_name, model.id, model.remark, "从xunjia_check中查看", DateTime.Now);
                            //        }
                            //        Utils.WriteTextLog("xunjia_check-" + id, "用户" + user_name + "查看该条信息；" + tips, DateTime.Now);
                            //    }
                            //}
                            if (uModel.group_id > 1)
                            {
                                if (uModel.readcountTotal >= rzcount) {
                                    strTxt.Append(",\"mobile\":\"请联系客服开通VIP\"");
                                    mobile += "总次数超过限制\"mobile\":\"请联系客服开通VIP\"";
                                    tips = "认证会员活动期可以体验查看" + group_1view111 + "次的权利,升级VIP 可以查看" + group_2view111 + "次,赶紧联系客服升级吧";
                                }
                                else
                                {
                                    if (uModel.readtime != null)
                                    {
                                        if (uModel.readtime.Date == DateTime.Now.Date)
                                        {
                                            if (uModel.readcount >= userViewCount)//认证会员 只能查看一次 
                                            {
                                                strTxt.Append(",\"mobile\":\"请联系客服开通VIP\"");
                                                mobile += "每天次数超过限制\"mobile\":\"请联系客服开通VIP\"";
                                                tips = "今日查看次数剩余 0";
                                                if (uModel.group_id == 2)
                                                    tips = "认证会员活动期可以体验每天查看" + group_1view + "次的权利,升级VIP 可以每天查看" + group_2view + "次,赶紧联系客服升级吧";
                                            }
                                            else
                                            {
                                                if (bll.CheckPhoneNum(id) == "error")
                                                {
                                                    strTxt.Append(",\"mobile\":\"请联系客服开通VIP\"");
                                                    ///Demon 20200307 修改提示文字 中国石材圈：@木风 查看30次后弹出信息，改成：该条委托采购订单已合作成功，谢谢查看！
                                                    mobile += "该条委托采购订单已合作成功，谢谢查看！\"mobile\":\"请联系客服开通VIP\"";
                                                    tips = "该条委托采购订单已合作成功，谢谢查看！";
                                                }
                                                else
                                                {
                                                    strTxt.Append(",\"mobile\":\"" + userModel.mobile + "\"");
                                                    mobile += "\"mobile\":\""+ userModel.mobile + "\"";
                                                    blluser.UpdateField(uModel.id, "readcount=readcount+1,readcountTotal=readcountTotal+1");
                                                    viewbll.AddLog(1, uModel.id, uModel.user_name, model.id, model.remark, "从xunjia_details中查看", DateTime.Now);
                                                }


                                            }
                                        }
                                        else
                                        {
                                            if (bll.CheckPhoneNum(id) == "error")
                                            {
                                                strTxt.Append(",\"mobile\":\"请联系客服开通VIP\"");
                                                mobile += "该条委托采购订单已合作成功，谢谢查看！\"mobile\":\"请联系客服开通VIP\"";
                                                tips = "该条委托采购订单已合作成功，谢谢查看！";
                                            }
                                            else
                                            {
                                                strTxt.Append(",\"mobile\":\"" + userModel.mobile + "\"");
                                                mobile += "\"mobile\":\"" + userModel.mobile + "\"";
                                                blluser.UpdateField(uModel.id, "readcount=1,readcountTotal=readcountTotal+1,readtime=getdate()");
                                                viewbll.AddLog(1, uModel.id, uModel.user_name, model.id, model.remark, "从xunjia_details中查看", DateTime.Now);
                                            }
                                        }
                                    }
                                    else
                                    {
                                        if (bll.CheckPhoneNum(id) == "error")
                                        {
                                            strTxt.Append(",\"mobile\":\"请联系客服开通VIP\"");
                                            mobile += "该条委托采购订单已合作成功，谢谢查看！\"mobile\":\"请联系客服开通VIP\"";
                                            tips = "该条委托采购订单已合作成功，谢谢查看！";
                                        }
                                        else
                                        {
                                            strTxt.Append(",\"mobile\":\"" + userModel.mobile + "\"");
                                            mobile += "\"mobile\":\""+ userModel.mobile + "\"";
                                            blluser.UpdateField(uModel.id, "readcount=1,readcountTotal=readcountTotal+1,readtime=getdate()");
                                            viewbll.AddLog(1, uModel.id, uModel.user_name, model.id, model.remark, "从xunjia_details中查看", DateTime.Now);
                                        }
                                    }
                                }
                            }
                            ///##### 20190403
                            ///##### 认证会员与VIP会员查看模式分离 
                            ///##### 认证会员启用 累计查看次数模式
                            ///##### 放弃原来每天查看的逻辑
                            ///##### Demon
                            //else if (uModel.group_id == 2) {
                            //    if (uModel.readcountTotal >= userViewCount)
                            //    {
                            //        strTxt.Append(",\"mobile\":\"请联系客服开通VIP\"");
                            //        if (uModel.group_id == 2)
                            //            tips = "认证会员活动期可以体验每天查看" + group_1view + "次的权利,升级VIP 可以每天查看" + group_2view + "次,赶紧联系客服升级吧";
                            //    }
                            //    else
                            //    {
                            //        strTxt.Append(",\"mobile\":\"" + userModel.mobile + "\"");
                            //        blluser.UpdateField(uModel.id, "readcount=readcount+1,readcountTotal=readcountTotal+1,readtime=getdate()");
                            //        tips = "您还有" + (userViewCount - uModel.readcountTotal) + "次查看机会可用！";
                            //    }
                            //}
                            #endregion

                            Utils.WriteTextLog("xunjia_details-" + uModel.mobile, "询价ID" + id + "用户" + user_name + "查看该条信息；"+ mobile + tips, DateTime.Now);
                        }



                    }
                }
            }
            else
            {
                strTxt.Append(",\"lianxiren\":\"\"");
                strTxt.Append(",\"avatar\":\"../image/default_avatar.png\"");
                strTxt.Append(",\"mobile\":\"\"");
            }
            strTxt.Append(",\"tips\":\"" + tips + "\"");
            if (!string.IsNullOrEmpty(user_name) && !string.IsNullOrEmpty(user_pwd) & uModel != null)
            {
                strTxt.Append(",\"cur_group_id\":" + uModel.group_id);
            }
            else
            {
                strTxt.Append(",\"cur_group_id\":0");
            }
            //循环图片列表
            //if (model.albums != null)
            //{
            //    strTxt.Append(",\"pic_list\":[");
            //    foreach (Model.xunjia_albums modelt in model.albums)
            //    {
            //        strTxt.Append("{");
            //        strTxt.Append("\"id\":" + modelt.id);
            //        strTxt.Append(",\"img_url\":\"http://" + modelt.original_path + "\"");
            //        strTxt.Append("},");
            //    }
            //    strTxt.Remove(strTxt.ToString().LastIndexOf(','), 1);
            //    strTxt.Append("]");
            //}
            //else
            //{
            //    strTxt.Append(",\"pic_list\":[]");
            //}

            string imglist = string.Empty;
            if (model.albums != null)
            {
                foreach (Model.xunjia_albums modelt in model.albums)
                {
                    if (modelt.original_path.Contains("shicaiquanzi"))
                    {
                        if (modelt.original_path.Contains("http://")) {
                            imglist += "<div class=\"pic\"><img src=\"" + modelt.original_path + "\"/></div>";
                            }
                        else { imglist += "<div class=\"pic\"><img src=\"http://" + modelt.original_path + "\"/></div>"; }
                    }
                    else
                    {
                        imglist += "<div class=\"pic\"><img src=\"http://pic.shicaiquanzi.com" + modelt.original_path + "\"/></div>";
                    }
                   
                }
            }
            if (!string.IsNullOrEmpty(imglist))
            {
                strTxt.Append(",\"imglist\":\"" + Microsoft.JScript.GlobalObject.escape(imglist) + "\"");
            }
            else
            {
                strTxt.Append(",\"imglist\":\"\"");
            }

            if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(user_pwd) || uModel==null || uModel.id!=model.user_id)
            {
                strTxt.Append(",\"baojia_list\":[]");
            }else {
                //推荐内容
                strTxt.Append(",\"baojia_list\":[");
                BLL.xunjia_baojia bll2 = new BLL.xunjia_baojia();
                DataSet ds = bll2.GetList(0, "status=0 and article_id=" + id, "id desc");
                //如果记录存在
                if (ds.Tables[0].Rows.Count > 0)
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr = ds.Tables[0].Rows[i];
                        strTxt.Append("{");
                        strTxt.Append("\"id\":" + dr["id"]);
                        strTxt.Append(",\"remark\":\"" + Microsoft.JScript.GlobalObject.escape(dr["remark"].ToString()) + "\"");
                        strTxt.Append(",\"add_time\":\"" + Utils.DateStringFromNow(Convert.ToDateTime(dr["add_time"])) + "\"");
                        int _user_id = int.Parse(dr["user_id"].ToString());
                        if (_user_id > 0)
                        {
                            string _username = new BLL.users().GetUserName(_user_id);
                            string _avatar = new BLL.users().GetAvatar(_user_id);
                            string _mobile = new BLL.users().GetMobile(_user_id);
                            strTxt.Append(",\"user_name\":\"" + _username + "\"");
                            if (string.IsNullOrEmpty(_avatar))
                            {
                                strTxt.Append(",\"avatar\":\"../image/default_avatar.png\"");
                            }
                            else if (_avatar.Contains("shicaiquanzi"))
                            {
                                strTxt.Append(",\"avatar\":\"http://" + _avatar + "\"");
                            }
                            else
                            {
                                strTxt.Append(",\"avatar\":\"http://pic.shicaiquanzi.com" + _avatar + "\"");
                            }
                            strTxt.Append(",\"mobile\":\"" + _mobile + "\"");
                        }
                        strTxt.Append("}");
                        //是否加逗号
                        if (i < ds.Tables[0].Rows.Count - 1)
                        {
                            strTxt.Append(",");
                        }
                    }
                }
                strTxt.Append("]");
            }
            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}